$(function() {
    WriteArticle();


});

// 写文章提交数据
function WriteArticle() {
    // 获取富文本内容
    var E = window.wangEditor;
    var editor = new E('#editor_text');
    editor.create();
    document.getElementById('write_article_button').addEventListener('click', function () {
        // 读取 html
        // alert(editor.txt.html())
        var editor_text = editor.txt.html()
        // console.log(editor_text)

        var article_name = $('#article_title').val();
        var article_classify = $('#article_classify').val();
        var token = localStorage.token || sessionStorage.token;


        params = {
            'article_name': article_name,
            'article_classify': article_classify,
            'article_text': editor_text,
        };
        console.log(article_name, article_classify, editor_text);

        $.ajax({
            url: host + "/write_article/",
            method: "post",
            data: JSON.stringify(params),
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                location.href = './my_article.html';

            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })



    }, false)
}
